package org.kernely.stream.dao;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.kernely.persistence.service.SessionService;
import org.kernely.stream.persistence.Stream;
import org.osgi.service.component.ComponentContext;

public class StreamDAO {
	/**
	 * SessionService service
	 */
	private static SessionService service;
	
	/**
	 * The stream dao instance
	 */
	private static StreamDAO instance;

	/**
	 * Store the stream dao instance 
	 * @return the stream dao
	 */
	public static StreamDAO getInstance() {
		return instance;
	}
	
	/**
	 * Set the persistence session service
	 * @param sessionService the session service to set
	 */
	public final void setPersistenceService(SessionService sessionService) {
		service = sessionService;
	}

	/**
	 * Activate the component, called by SC runtime
	 * @param ctxt the activation context
	 */
	protected final void activate(ComponentContext ctxt) {
		instance = this;
	}
	
	/**
	 * Gets the session service.
	 * 
	 * @return the session service
	 */
	public static SessionService getService() {
		return service;
	}

	/**
	 * Sets the session service.
	 * 
	 * @param service
	 *            the session service to set
	 */
	public static void setService(SessionService service) {
		StreamDAO.service = service;
	}
	
	public final Set<Stream> getAllStream(){
		Session session = service.getSessionFactory().getCurrentSession();
		Transaction t = session.beginTransaction();
		Set<Stream> streams = new HashSet<Stream>(session.createCriteria(Stream.class).list());
		t.commit();
		if (session.isOpen()) {
			session.close();
		}
		return streams ;
	}
	
	public final Stream findStreamById(int id){
		Session session = service.getSessionFactory().getCurrentSession();
		Transaction t = session.beginTransaction();
		Criteria crit = session.createCriteria(Stream.class);
		crit.add(Restrictions.like("id", id));
		Stream s = (Stream) crit.list().get(0);
		t.commit();
		if (session.isOpen()) {
			session.close();
		}
		return s;
	}
	
	public final void saveStream(Stream s){
		Session session = service.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		session.save(s);
		t.commit();
		session.flush();
		if (session.isOpen()) {
			session.close();
		}
	}
	
	public final void updateStream(Stream s){
		Session session = service.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		session.update(s);
		t.commit();
		session.flush();
		if (session.isOpen()) {
			session.close();
		}
	}
	
	/**
	 * Find a stream, using his uid and his category
	 * @param category The category of the stream (for example "Streams/Users").
	 * @param uid The uid of the stream.
	 * @return
	 */
	public final Stream findStream(String category, String uid){
		Session session = service.getSessionFactory().getCurrentSession();
		Transaction t = session.beginTransaction();
		Criteria crit = session.createCriteria(Stream.class);
		crit.add(Restrictions.like("uid", uid));
		crit.add(Restrictions.like("category", category));
		List result = crit.list();
		Stream s = null;
		if (result != null){
			s = (Stream) crit.list().get(0);			
		}
		t.commit();
		if (session.isOpen()) {
			session.close();
		}
		return s;
	}
}
